﻿<%@ WebHandler Language="C#" CodeBehind="SystemHandler.ashx.cs" Class="GovernmentOfficeProject.handler.SystemHandler" %>

using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelBusinessLogic;
using System.Web.SessionState;
using System.Collections.Generic;

namespace GovernmentOfficeProject.handler
{
    public class SystemHandler : IHttpHandler,IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0)
            {
                context.Response.Redirect("~/Login.htm?FromUrl=" + HttpUtility.UrlEncode(context.Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
            }
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["Action"];
            string ret = "";
            switch(action)
            {
                case "modifyInfo":ret = GetCurrentUser();break;
                case "saveUserInfo": ret = SaveUserInfo(); break;
                case "saveNewPassword": ret = SaveNewPassword(); break;
            }
            context.Response.Write(ret);
        }
        
        /// <summary>
        /// 获得当前登录用户信息
        /// </summary>
        /// <returns></returns>
        public string GetCurrentUser()
        {
            HttpContext context = System.Web.HttpContext.Current;
            BLL_USER bllUser = new BLL_USER();
            Entity_USER_User userEntity = new Entity_USER_User();
            userEntity.UserID = SystemContext.CurrentUserID;
            return JsonHelper.ToJson(bllUser.getUserByUser(userEntity));
        }
        
        /// <summary>
        /// 保存当前用户信息   用户基本信息修改
        /// </summary>
        /// <returns></returns>
        public string SaveUserInfo()
        {
            HttpContext context = System.Web.HttpContext.Current;
            Entity_USER_User user = new Entity_USER_User();
            user.RealName = context.Request.Params["RealName"];
            user.Sex = context.Request.Params["Sex"];
            user.MobilePhone = context.Request.Params["MobilePhone"];
            user.Telephony = context.Request.Params["Telephony"];
            user.QQ = context.Request.Params["QQ"];
            user.Email = context.Request.Params["Email"];
            user.HomeAddress = context.Request.Params["HomeAddress"];
            user.OfficeAddress = context.Request.Params["OfficeAddress"];
            user.ModifyUserID = SystemContext.CurrentUserID;
            user.ModifyDateTime = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
            BLL_USER bllUser = new BLL_USER();

            return JsonHelper.ToJson(bllUser.updateUserByUserID(SystemContext.CurrentUserID, user));
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public bool VerificationOldPassword()
        {
            bool isSuccess = false;
            HttpContext context = System.Web.HttpContext.Current;
            BLL_USER bllUser = new BLL_USER();
            Entity_USER_User userEntity = new Entity_USER_User();
            userEntity.UserID = SystemContext.CurrentUserID;
            userEntity.LoginPassword = context.Request.Params["OldPassword"];
            List<Entity_USER_User> userList = bllUser.getUserListByCondition(userEntity);
            if(userList != null && userList.Count > 0)
            {
                isSuccess = true;
            }
            return isSuccess;
        }
        
        /// <summary>
        /// 保存新密码
        /// </summary>
        /// <returns></returns>
        public string SaveNewPassword()
        {
            Dictionary<string, object> result = new Dictionary<string, object>();
            if(!VerificationOldPassword())
            {
                result.Add("isSuccess",false);
                result.Add("ErrMsg", "旧密码错误，请重新输入");
                return JsonHelper.ToJson(result);
            }
            HttpContext context = System.Web.HttpContext.Current;
            BLL_USER bllUser = new BLL_USER();
            Entity_USER_User userEntity = new Entity_USER_User();
            userEntity.UserID = SystemContext.CurrentUserID;
            userEntity.LoginPassword = context.Request.Params["NewPassword"];
            bool isSuccess = bllUser.updateUserByUserID(userEntity.UserID, userEntity);
            if(isSuccess)
            {
                result.Add("isSuccess", true);
            }
            else
            {
                result.Add("isSuccess", false);
                result.Add("ErrMsg", "保存失败，请与管理员联系");
            }
            return JsonHelper.ToJson(result);
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

